package org.ucas.kgraph.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.ucas.kgraph.dto.*;
import org.ucas.kgraph.entity.Account;
import org.ucas.kgraph.entity.Relation;
import org.ucas.kgraph.entity.RelationType;
import org.ucas.kgraph.entity.Schem;
import org.ucas.kgraph.entity.Term;
import org.ucas.kgraph.service.RelationService;
import org.ucas.kgraph.service.SchemService;
import org.ucas.kgraph.utils.exception.BadRequestException;
import org.ucas.kgraph.utils.exception.ConflictException;
import org.ucas.kgraph.utils.exception.NotFoundException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

@RestController
public class SchemController extends BaseController {

  @Autowired
  private SchemService schemService;
  
  
  
  
  @RequestMapping(value = "/schems/roots",method = RequestMethod.GET)
  public ListDto<SchemDto> roots(@RequestParam(value = "file_id",required = false) Long file_id,@RequestParam(value = "account_id",required = false) Long account_id, @RequestParam("type") String type,@RequestParam(value = "state",required = false) String state){
	  ListDto<SchemDto> shemou;

	  if(account_id==null){
		  return ListDto.of(schemService.listRootAll(type,state,file_id),SchemDto::ofroot);
	  }else{
		  return ListDto.of(schemService.listRootByUser(account_id,type,state,file_id),SchemDto::ofroot);
	  }
  }
  
  @RequestMapping(value = "/schems/rootslistbySearch",method = RequestMethod.GET)
  public ListDto<SchemDto> rootsBySearch(@RequestParam(value = "file_id",required = false) Long file_id,@RequestParam(value = "account_id",required = false) Long account_id, @RequestParam("type") String type,@RequestParam(value = "state",required = false) String state,@RequestParam("searchString") String searchString){
	  ListDto<SchemDto> shemou;

	  
	  return ListDto.of(schemService.listRootBySearch(account_id,type,state,file_id,searchString),SchemDto::ofroot);
	  
  }
  
  @RequestMapping(value = "/schems/{id:\\d+}/deletesp",method = RequestMethod.DELETE)
  public ResponseDto deletesp(@PathVariable Long id){
    schemService.delete(id);
    return ResponseDto.OK;
  }
  @RequestMapping(value = "/schems/{id:\\d+}/deletesm",method = RequestMethod.DELETE)
  public ResponseDto deletesm(@PathVariable Long id){
    schemService.delete(id);
    return ResponseDto.OK;
  }
  @RequestMapping(value = "/schems/{id:\\d+}/deletep",method = RequestMethod.DELETE)
  public ResponseDto deletep(@PathVariable Long id){
    schemService.delete(id);
    return ResponseDto.OK;
  }
  
  
  @RequestMapping(value = "/schems",method = RequestMethod.POST)
  public CarryDto<SchemDto> create( HttpServletRequest request){
	SchemDto dto = SchemDto.from(request);
    Schem tmp = new Schem();
    dto.writeTo(tmp);
    tmp.setFile_id(file());
    schemService.create(tmp);
    dto.id = tmp.getId();
    return CarryDto.of(dto.of(tmp));
  }
  @RequestMapping(value = "/schems/pcreate",method = RequestMethod.POST)
  public CarryDto<SchemDto> pcreate( HttpServletRequest request){
	SchemDto dto = SchemDto.from(request);
    Schem tmp = new Schem();
    dto.writeTo(tmp);
    tmp.setFile_id(file());
    schemService.create(tmp);
    dto.id = tmp.getId();
    return CarryDto.of(dto.of(tmp));
  }
  @RequestMapping(value = "/schems/save",method = RequestMethod.POST)
  public CarryDto<SchemDto> save( HttpServletRequest request){
	SchemDto dto = SchemDto.from(request);
    Schem tmp = new Schem();
    dto.writeTo(tmp);
    tmp.setFile_id(file());
    schemService.create(tmp);
    dto.id = tmp.getId();
    return CarryDto.of(dto.of(tmp));
  }
  
  @RequestMapping(value = "/schems/roots/select",method = RequestMethod.GET)
  public ResponseDto selectRoot(@RequestParam("selfrootId") Long selfrootId){
    selfroot(selfrootId);
    return ResponseDto.OK;
  }
  @RequestMapping(value = "/schems/get",method = RequestMethod.GET)
  public CarryDto<SchemDto> getGraphById(){
	Long id = selfroot();
    return CarryDto.of(SchemDto.of(schemService.load(id)));
  }
  @RequestMapping(value = "/schems/search/draw",method = RequestMethod.GET)
  public CarryDto<SchemDto> getsearchGraphById(){
	Long id = searchroot();
    return CarryDto.of(SchemDto.of(schemService.load(id)));
  }
  
  @RequestMapping(value = "/schems/search",method = RequestMethod.GET)
  public ListDto<SchemDto> searchByRoot(@RequestParam("root") String root,@RequestParam("type") String type,@RequestParam("state") String state){
    return ListDto.of(schemService.searchByRoot(root,type,state),SchemDto::ofroot);
  }
  
  @RequestMapping(value = "/schems/roots/search",method = RequestMethod.GET)
  public ResponseDto selectSearchRoot(@RequestParam("searchroot") Long rootId){
    searchroot(rootId);
    return ResponseDto.OK;
  }
  
  @RequestMapping(value = "/schems/{id:\\d+}/recontent",method = RequestMethod.PUT)
  public CarryDto<SchemDto> recontent(@PathVariable Long id,@RequestParam("content") String content,@RequestParam("root") String root,@RequestParam("theme") String theme) {
	Schem schem = schemService.recontent(id,content,root,theme);
   
    return CarryDto.of(schem);
  }
  
  @RequestMapping(value = "/schems/{id:\\d+}/reroot",method = RequestMethod.PUT)
  public CarryDto<SchemDto> reroot(@PathVariable Long id,@RequestParam("newroot") String newroot) throws JsonParseException, JsonMappingException, IOException {
    Schem schem = schemService.reroot(id,newroot);
    return CarryDto.of(schem);
  }
  @RequestMapping(value = "/schems/{id:\\d+}/submitp",method = RequestMethod.PUT)
  public CarryDto<SchemDto> submitp(@PathVariable Long id,@RequestParam("file_id") Long file_id) {
	Schem tmp = schemService.load(id);
	Schem schem = schemService.submitp(id,file_id);
	return CarryDto.of(schem);
  }
  
  @RequestMapping(value = "/schems/{id:\\d+}/movep",method = RequestMethod.PUT)
  public CarryDto<SchemDto> movep(@PathVariable Long id,@RequestParam("file_id") Long file_id) {
	Schem tmp = schemService.load(id);
	if(file_id.equals(file())) throw NotFoundException.Term_NotExists;
	Schem schem = schemService.submitp(id,file_id);
	return CarryDto.of(schem);
  }
  
}
